package com.skyline.user.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.skyline.base.dao.impl.BaseDaoImpl;
import com.skyline.base.type.Activity;
import com.skyline.base.type.IdolType;
import com.skyline.common.bean.Page;
import com.skyline.user.dao.FanDao;
import com.skyline.user.mapper.FanMapper;
import com.skyline.user.model.Fan;

@Repository("fanDao")
public class FanDaoImpl extends BaseDaoImpl implements FanDao {
	/**
	 * insert into fan
	 * (idolid,idolnickname,idolportrait,fanid,fannickname,fanportrait
	 * ,idoltype,createtime) values (?,?,?,?,?,?,?,current_timestamp)
	 */
	@Value("${FanDao.insertFansAndIdol}")
	private String insertFansAndIdolSQL;
	/**
	 * update fan set activity = ? where idolid= ? and fanid= ?
	 */
	@Value("${FanDao.updateFanActivity}")
	private String updateFanActivitySQL;
	
	@Value("${FanDao.updateFanAttentionType}")
	private String updateFanAttentionTypeSQL;

	/**
	 * select * from fan where idolid= ? and activity= ?
	 */
	@Value("${FanDao.queryFansByIdolId}")
	private String queryFansByIdolIdSQL;
	/**
	 * select * from fan where fanid= ? and activity= ?
	 */
	@Value("${FanDao.queryIdolsByFanId}")
	private String queryIdolsByFanIdSQL;

	/**
	 * select * from fan where fanid= ? and idoltype= ? and activity= ?
	 */
	@Value("${FanDao.queryIdolsByFanIdAndIdolType}")
	private String queryIdolsByFanIdAndIdolTypeSQL;

	/**
	 * select count(1) from fan where idolid= ? and fanId=? and activity= ?
	 */
	@Value("${FanDao.existAttention}")
	private String existAttentionSql;

	@Override
	public void insertIdolAndFans(Long idolId, String idolNickname,
			String idolPortrait, Long fanId, String fanNickname,
			String fanPortrait, IdolType idolType,boolean attentionType) {
		jdbcTemplate.update(insertFansAndIdolSQL, idolId, idolNickname,
				idolPortrait, fanId, fanNickname, fanPortrait,
				idolType.toString(),attentionType);
	}

	@Override
	public List<Fan> queryFansByIdolId(Long idolId, Page page, Activity activity) {
		return this.getPaginationResult(queryFansByIdolIdSQL, page,
				FanMapper.getMapper(), idolId, activity.toString());
	}

	@Override
	public List<Fan> queryIdolsByFanId(Long fanId, Page page, Activity activity) {
		return this.getPaginationResult(queryIdolsByFanIdSQL, page,
				FanMapper.getMapper(), fanId, activity.toString());

	}

	@Override
	public List<Fan> queryIdolsByFansIdAndIdolType(Long fanId,
			IdolType idolType, Page page, Activity activity) {

		if (idolType == null) {
			List<Fan> fans = this.getPaginationResult(queryIdolsByFanIdSQL,
					page, FanMapper.getMapper(), fanId, activity.toString());
			return fans;
		} else {
			// long t1=System.currentTimeMillis();
			List<Fan> fans = this.getPaginationResult(
					queryIdolsByFanIdAndIdolTypeSQL, page,
					FanMapper.getMapper(), fanId, idolType.toString(),
					activity.toString());
			// long t2=System.currentTimeMillis();
			// System.out.println(t2-t1);
			return fans;
		}
	}

	@Override
	public boolean existAttention(Long idolId, Long fanId, Activity activity) {
		if (jdbcTemplate.queryForInt(existAttentionSql, idolId, fanId,
				activity.toString()) == 0) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public int countFansByIdolId(Long idolId) {
		return getCountResult(queryFansByIdolIdSQL, idolId,
				Activity.NORMAL.toString());
	}

	@Override
	public int countIdolByFansIdAndIdolType(Long fanId, IdolType idolType) {
		return getCountResult(queryIdolsByFanIdAndIdolTypeSQL, fanId,
				idolType.toString(), Activity.NORMAL.toString());
	}

	@Override
	public void deleteIdol(Long idolId, Long fanId) {
		// Activity activity=Activity.deleted;
		jdbcTemplate.update(updateFanActivitySQL, Activity.DELETED.toString(),
				idolId, fanId);
	}

	@Override
	public void recoverAttention(Long idolId, Long fanId) {
		jdbcTemplate.update(updateFanActivitySQL, Activity.NORMAL.toString(),
				idolId, fanId);

	}

	@Override
	public void updateAttentionType(Long idolId, Long fanId,boolean attentionType) {
		jdbcTemplate.update(updateFanAttentionTypeSQL, attentionType,idolId,fanId);
		
	}

}
